local weiting = fk.CreateSkill{
  name = "lz__weiting",
  tags = { "Phase" }
}


Fk:loadTranslationTable{
  ["lz__weiting"] = "危庭",
  [":lz__weiting"] = "出牌阶段每项各一次，若你于此阶段造成过伤害/回复过体力/摸过牌，你可从弃牌堆中获得一张基本/锦囊/装备牌，若如此做，此阶段结束时，你失去未执行项的体力。",
  ["#lz__weiting"] = "请选择要执行的操作",
  ["weiting_basic"] = "获得基本牌",
  ["weiting_jinnang"] = "获得锦囊牌",
  ["weiting_equip"] = "获得装备牌",
}

-- 跟踪摸牌
weiting:addEffect(fk.AfterCardsMove, {
  can_refresh = function(self, event, target, player, data)
    if player:hasSkill(weiting.name) and player.phase == Player.Play then
      for _, move in ipairs(data) do
        if move.to == player and move.moveReason == fk.ReasonDraw then
          return true
        end
      end
    end
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:addPlayerMark(player, "weiting_draw-phase")
  end,
})

-- 跟踪造成伤害
weiting:addEffect(fk.Damage, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(weiting.name) and player.phase == Player.Play and data.from and data.from == player
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:addPlayerMark(player, "weiting_damage-phase")
  end,
})

-- 跟踪回复体力
weiting:addEffect(fk.HpRecover, {
  can_refresh = function(self, event, target, player, data)
    return player:hasSkill(weiting.name) and player.phase == Player.Play and target == player
  end,
  on_refresh = function(self, event, target, player, data)
    player.room:addPlayerMark(player, "weiting_recover-phase")
  end,
})

-- 主动技能效果
weiting:addEffect("active", {
  anim_type = "drawcard",
  interaction = function(self, player)
    local choices = {}
    if player:getMark("weiting_damage-phase") >= 1 and player:getMark("weiting_basic-phase") == 0 then
      table.insert(choices, "weiting_basic")
    end
    if player:getMark("weiting_recover-phase") >= 1 and player:getMark("weiting_trick-phase") == 0 then
      table.insert(choices, "weiting_jinnang")
    end
    if player:getMark("weiting_draw-phase") >= 1 and player:getMark("weiting_equip-phase") == 0 then
      table.insert(choices, "weiting_equip")
    end
    return UI.ComboBox{choices = choices}
  end,
  can_use = function(self, player)
    local choices = {}
    if player:getMark("weiting_damage-phase") >= 1 and player:getMark("weiting_basic-phase") == 0 then
      table.insert(choices, "basic")
    end
    if player:getMark("weiting_recover-phase") >= 1 and player:getMark("weiting_trick-phase") == 0 then
      table.insert(choices, "jinnang")
    end
    if player:getMark("weiting_draw-phase") >= 1 and player:getMark("weiting_equip-phase") == 0 then
      table.insert(choices, "damage")
    end
    return #choices > 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local choice = self.interaction.data
    local card_type, mark_type = "", ""
    if choice == "weiting_basic" then
      card_type = Card.TypeBasic
      mark_type = "weiting_basic-phase"
    elseif choice == "weiting_jinnang" then
      card_type = Card.TypeTrick
      mark_type = "weiting_trick-phase"
    elseif choice == "weiting_equip" then
      card_type = Card.TypeEquip
      mark_type = "weiting_equip-phase"
    end
    room:addPlayerMark(player, mark_type)
    local discardPile = table.simpleClone(room.discard_pile)
    local targetCards = table.filter(discardPile, function(id)
      return Fk:getCardById(id).type == card_type
    end)
    -- 如果有符合条件的牌，让玩家选择一张
    if #targetCards > 0 then
      local card = room:askToChooseCard(player, {
        target = player,
        flag = { card_data = {{ weiting.name, targetCards }} },
        skill_name = weiting.name,
      })
      room:moveCardTo(card, Card.PlayerHand, player, fk.ReasonJustMove, weiting.name, nil, true, player)
    end
  end,
})

-- 出牌阶段结束时，失去未执行项的体力
weiting:addEffect(fk.EventPhaseEnd, {
  is_delay_effect = true,
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(weiting.name) and player.phase == Player.Play
    and player:usedSkillTimes(weiting.name, Player.HistoryPhase) > 0
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local damageCount = 0
    if player:getMark("weiting_damage-phase") == 0 then
       damageCount = damageCount + 1
    end
    if player:getMark("weiting_recover-phase") == 0 then
       damageCount = damageCount + 1
    end
    if player:getMark("weiting_draw-phase") == 0 then
       damageCount = damageCount + 1
    end
    room:loseHp(player, damageCount, weiting.name)
  end,
})

return weiting